package utils

import (
	"fmt"
	"regexp"
	"strings"
)

// 监听地址正则匹配
var adddressReg = regexp.MustCompile(`^\w+://[\w\d\.:\[\]]+$`)

// IP地址解析 类似`tcp://127.0.0.1:8888`
func ParseAddress(str string) (network, address string, err error) {
	if !adddressReg.MatchString(str) {
		err = fmt.Errorf("%s match failed", str)
		return
	}

	str = strings.ToLower(str)

	list := strings.Split(str, "://")
	network = strings.ToLower(list[0])
	address = strings.ToLower(list[1])

	return
}
